package com.example.picturebacked.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.picturebacked.annotation.AuthCheck;
import com.example.picturebacked.common.BaseResponse;
import com.example.picturebacked.common.DeleteRequest;
import com.example.picturebacked.common.ResultUtils;
import com.example.picturebacked.contant.UserContant;
import com.example.picturebacked.exception.BusinessException;
import com.example.picturebacked.exception.ErrorCode;
import com.example.picturebacked.exception.ThrowUtils;
import com.example.picturebacked.manager.auth.annotation.SaSpaceCheckPermission;
import com.example.picturebacked.manager.auth.model.SpaceUserPermissionConstant;
import com.example.picturebacked.model.dto.space.*;
import com.example.picturebacked.model.dto.spaceuser.SpaceUserAddRequest;
import com.example.picturebacked.model.dto.spaceuser.SpaceUserEditRequest;
import com.example.picturebacked.model.dto.spaceuser.SpaceUserQueryRequest;
import com.example.picturebacked.model.entity.Space;
import com.example.picturebacked.model.entity.SpaceUser;
import com.example.picturebacked.model.entity.User;
import com.example.picturebacked.model.enums.SpaceLevelEnum;
import com.example.picturebacked.model.vo.SpaceUserVO;
import com.example.picturebacked.model.vo.SpaceVO;
import com.example.picturebacked.service.SpaceService;
import com.example.picturebacked.service.SpaceUserService;
import com.example.picturebacked.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/spaceUser")
@Api(tags = "团队空间接口")
public class SpaceUserController {

    @Resource
    private SpaceService spaceService;

    @Resource
    private UserService userService;

    @Resource
    private SpaceUserService spaceUserService;

    /**
     * 增加团队空间
     */
    @PostMapping("/add")
    @ApiOperation("空间增加团队成员")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Long> addSpace(@RequestBody SpaceUserAddRequest spaceAddRequest) {
        ThrowUtils.throwIf(spaceAddRequest == null, ErrorCode.PARAMS_ERROR);
        long id = spaceUserService.addSpaceUser(spaceAddRequest);
        return ResultUtils.success(id);
    }

    /**
     * 删除空间成员
     */
    @PostMapping("/delete")
    @ApiOperation("用户删除空间")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = deleteRequest.getId();
        // 判断团队空间是否存在
        Space deletespace = spaceService.getById(id);
        ThrowUtils.throwIf(deletespace == null, ErrorCode.NOT_FOUND_ERROR);
        // 删除空间接收返回值
        boolean result = spaceUserService.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据id获取空间
     */
    @GetMapping("/get")
    @ApiOperation("查询某个成员在某个空间的信息")
    public BaseResponse<SpaceUser> getSpaceById(@RequestBody SpaceUserQueryRequest spaceUserQueryRequest) {
        ThrowUtils.throwIf(spaceUserQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Long spaceId = spaceUserQueryRequest.getSpaceId();
        Long userId = spaceUserQueryRequest.getUserId();
        ThrowUtils.throwIf(ObjectUtil.hasEmpty(spaceId, userId), ErrorCode.PARAMS_ERROR);
        SpaceUser spaceUser = spaceUserService.getOne(spaceUserService.getQueryWrapepr(spaceUserQueryRequest));
        ThrowUtils.throwIf(spaceUser == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(spaceUser);
    }

    /**
     * 查询成员信息列表
     */
    @PostMapping("/list")
    @ApiOperation("查询成员信息列表")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<List<SpaceUserVO>> listSpaceUser(@RequestBody SpaceUserQueryRequest spaceUserQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUserQueryRequest == null, ErrorCode.PARAMS_ERROR);
        List<SpaceUser> spaceUserList = spaceUserService.list(
                spaceUserService.getQueryWrapepr(spaceUserQueryRequest
                ));
        return ResultUtils.success(spaceUserService.getSpaceUserVOList(spaceUserList));
    }


    /**
     * 编辑空间成员信息
     */
    @PostMapping("/edit")
    @ApiOperation("编辑成员信息（设置权限）")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> editSpace(@RequestBody SpaceUserEditRequest spaceUserEditRequest,
                                           HttpServletRequest request) {
        if (spaceUserEditRequest == null || spaceUserEditRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SpaceUser spaceUser = new SpaceUser();
        BeanUtil.copyProperties(spaceUserEditRequest, spaceUser);
        // 检验数据信息
        spaceUserService.validSpaceUser(spaceUser, false);

        // 判断空间是否存在
        Long id = spaceUserEditRequest.getId();
        SpaceUser oldSpaceUser = spaceUserService.getById(id);
        ThrowUtils.throwIf(oldSpaceUser == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = spaceUserService.updateById(spaceUser);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 查询我加入的团队空间列表
     */
    @PostMapping("/list/my")
    @ApiOperation("查询我加入的团队空间列表")
    public BaseResponse<List<SpaceUserVO>> listMyTeamSpace(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        SpaceUserQueryRequest spaceUserQueryRequest = new SpaceUserQueryRequest();
        spaceUserQueryRequest.setUserId(loginUser.getId());
        List<SpaceUser> spaceUserList = spaceUserService.list(
                spaceUserService.getQueryWrapepr(spaceUserQueryRequest)
        );
        return ResultUtils.success(spaceUserService.getSpaceUserVOList(spaceUserList));
    }
}
































